package com.ssy.lingxi.order.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.ssy.lingxi.order.handler.converter.JpaJsonToListStringConverter;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 订单支付记录
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-17
 */
@Entity
@Table(schema = "public", name = "ord_payment", indexes = {@Index(name = "ord_payment_order_id_idx", columnList = "order_id"), @Index(name = "ord_payment_pay_time_idx", columnList = "payTime"), @Index(name = "ord_payment_serial_no_idx", columnList = "serialNo"), @Index(name = "ord_payment_batch_no_idx", columnList = "batchNo"), @Index(name = "ord_payment_fund_mode_idx", columnList = "fundMode"), @Index(name = "ord_payment_amount_idx", columnList = "payAmount"), @Index(name = "ord_payment_outer_status_idx", columnList = "outerStatus")})
public class OrderPaymentDO implements Serializable {
    private static final long serialVersionUID = -5485210707544415772L;

    /**
     * 主键Id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ord_payment_seq")
    @SequenceGenerator(sequenceName = "ord_payment_seq", name = "ord_payment_seq", allocationSize = 1)
    private Long id;

    /**
     * 多对一双向关联订单
     */
    @JsonBackReference
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id")
    private OrderDO order;

    /**
     * 支付时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime payTime;

    /**
     * 确认支付到账时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime confirmTime;

    /**
     * 支付批次
     */
    @Column
    private Integer serialNo;

    /**
     * 支付次数
     */
    @Column
    private Integer batchNo;

    /**
     * 支付类型，定义在OrderPayTypeEnum中
     */
    @Column
    private Integer payType;

    /**
     * 支付渠道，定义在OrderPayChannelEnum中
     */
    @Column
    private Integer payChannel;

    /**
     * 资金归集模式，定义在 FundModeEnum 中
     */
    @Column
    private Integer fundMode;

    /**
     * 支付环节
     */
    @Column(columnDefinition = "varchar(50)")
    private String payNode;

    /**
     * 支付比例
     */
    @Column(columnDefinition = "numeric(8,4)")
    private BigDecimal payRate;

    /**
     * 支付金额
     */
    @Column(columnDefinition = "numeric")
    private BigDecimal payAmount;

    /**
     * 采购商内部状态
     */
    @Column
    private Integer buyerInnerStatus;

    /**
     * 供应商内部状态
     */
    @Column
    private Integer vendorInnerStatus;

    /**
     * 外部状态
     */
    @Column
    private Integer outerStatus;

    /**
     * 结算状态，定义在 OrderPaymentSettlementStatusEnum 中
     */
    @Column
    private Integer settlementStatus;

    /**
     * 在线支付的商户订单号，用于退款
     */
    @Column(columnDefinition = "varchar(50)")
    private String tradeNo;

    /**
     * 订单服务生成的退款单号
     */
    @Column(columnDefinition = "varchar(50)")
    private String refundNo;

    /**
     * 支付凭证文件
     */
    @Convert(converter = JpaJsonToListStringConverter.class)
    @Column(columnDefinition = "jsonb")
    private List<String> vouchers;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public OrderDO getOrder() {
        return order;
    }

    public void setOrder(OrderDO order) {
        this.order = order;
    }

    public LocalDateTime getPayTime() {
        return payTime;
    }

    public void setPayTime(LocalDateTime payTime) {
        this.payTime = payTime;
    }

    public LocalDateTime getConfirmTime() {
        return confirmTime;
    }

    public void setConfirmTime(LocalDateTime confirmTime) {
        this.confirmTime = confirmTime;
    }

    public Integer getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(Integer serialNo) {
        this.serialNo = serialNo;
    }

    public Integer getBatchNo() {
        return batchNo;
    }

    public void setBatchNo(Integer batchNo) {
        this.batchNo = batchNo;
    }

    public Integer getPayType() {
        return payType;
    }

    public void setPayType(Integer payType) {
        this.payType = payType;
    }

    public Integer getPayChannel() {
        return payChannel;
    }

    public void setPayChannel(Integer payChannel) {
        this.payChannel = payChannel;
    }

    public Integer getFundMode() {
        return fundMode;
    }

    public void setFundMode(Integer fundMode) {
        this.fundMode = fundMode;
    }

    public String getPayNode() {
        return payNode;
    }

    public void setPayNode(String payNode) {
        this.payNode = payNode;
    }

    public BigDecimal getPayRate() {
        return payRate;
    }

    public void setPayRate(BigDecimal payRate) {
        this.payRate = payRate;
    }

    public BigDecimal getPayAmount() {
        return payAmount;
    }

    public void setPayAmount(BigDecimal payAmount) {
        this.payAmount = payAmount;
    }

    public Integer getBuyerInnerStatus() {
        return buyerInnerStatus;
    }

    public void setBuyerInnerStatus(Integer buyerInnerStatus) {
        this.buyerInnerStatus = buyerInnerStatus;
    }

    public Integer getVendorInnerStatus() {
        return vendorInnerStatus;
    }

    public void setVendorInnerStatus(Integer vendorInnerStatus) {
        this.vendorInnerStatus = vendorInnerStatus;
    }

    public Integer getOuterStatus() {
        return outerStatus;
    }

    public void setOuterStatus(Integer outerStatus) {
        this.outerStatus = outerStatus;
    }

    public Integer getSettlementStatus() {
        return settlementStatus;
    }

    public void setSettlementStatus(Integer settlementStatus) {
        this.settlementStatus = settlementStatus;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public String getRefundNo() {
        return refundNo;
    }

    public void setRefundNo(String refundNo) {
        this.refundNo = refundNo;
    }

    public List<String> getVouchers() {
        return vouchers;
    }

    public void setVouchers(List<String> vouchers) {
        this.vouchers = vouchers;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        OrderPaymentDO that = (OrderPaymentDO) o;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
